/*
 * @(#) CorsFilter
 *
 * <br> Copyright:  Copyright (c) 2017
 * <br> @author caijinhuang
 * <br> 2017-12-18 16:18:34
 */

package com.sunsharing.sharemeeting.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

/**
 * 实现前后端跨域请求
 * @author caijinhuang
 *
 * @time 2017年9月16日
 */
public class CorsFilter implements Filter {

    public void init(FilterConfig filterConfig) throws ServletException {
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
        // HttpServletResponse httpResponse = (HttpServletResponse) response;
        // httpResponse.setHeader("Access-Control-Allow-Origin", "http://localhost:8081");//设置允许跨域的域名
        // httpResponse.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
        // httpResponse.setHeader("Access-Control-Allow-Methods", "GET, PUT, DELETE, POST");//允许跨域的请求方式
        // /**
        //  * ajax请求的时候如果带有xhrFields:{withCredentials:true}，
        //  * 那么服务器后台在配置跨域的时候就必须要把Access-Control-Allow-Credentials这个请求头加上去
        //  */
        // httpResponse.setHeader("Access-Control-Allow-Credentials", "true");
        // httpResponse.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // 支持HTTP 1.1.
        // httpResponse.setHeader("Pragma", "no-cache"); // 支持HTTP 1.0. response.setHeader("Expires", "0");
        chain.doFilter(request, response);
    }

    public void destroy() {
        // TODO Auto-generated method stub
    }
}
